import re

def simple():
    """
    初识
    :return:
    """
    result = re.match("wangdao", "wangdao.cn")
    if result:
        print(result.group())


def single():
    """
    匹配单个字符
    :return:
    """
    ret = re.match(".", "M")
    print(ret.group())

    ret = re.match("t.", "too")
    print(ret.group())

    ret = re.match("t.o", "two")
    print(ret.group())
    print('-' * 50)
    ret = re.match("[hH]", "hello Python")
    print(ret.group())
    ret = re.match("[hH]", "Hello Python")
    print(ret.group())
    ret = re.match("[hH]ello Python", "Hello Python")
    print(ret.group())

    # 匹配0到9的第二种写法
    ret = re.match("[0-9]Hello Python", "6Hello Python" )
    print(ret.group())
    # 0-3 5-9
    ret = re.match("[0-35-9]Hello Python", "7Hello Python" )
    print(ret.group())
    print('-' * 50)
    ret = re.match(r"嫦娥\d号", "嫦娥1号发生成功")
    print(ret.group())

    ret = re.match(r"嫦娥\d号", "嫦娥2号发生成功")
    print(ret.group())

    ret = re.match(r"嫦娥\d号", "嫦娥3号发生成功")
    print(ret.group())



def more_alp():
    """
    匹配多个字符
    :return:
    """
    ret = re.match("[A-Z][a-z]*", "M")
    print(ret.group())

    ret = re.match("[A-Z][a-z]*", "MnnM")
    print(ret.group())

    ret = re.match("[A-Z][a-z]*", "Aabcdef")
    print(ret.group())
    print('-' * 50)
    names = ["name1", "_name", "2_name", "__name__"]
    for name in names:
        ret = re.match(r'[a-zA-Z]\w*', name)
        if ret:
            print(f'{name} 是正确的')
        else:
            print(f'{name} 不是正确的')
    print('-' * 50)
    ret = re.match(r"[1-9]?[0-9]", "7")
    print(ret.group())

    ret = re.match(r"[0-9]?\d", "33")
    print(ret.group())

    ret = re.match(r"[0-9]?\d", "09")
    print(ret.group())

    print('-' * 50)
    ret = re.match("[a-zA-Z0-9]{6}", "12a3g45678")
    print(ret.group())
    ret = re.match("[a-zA-Z0-9]{8,10}", "1ad12f23s*34455ff66")
    print(ret.group())

def start_end():
    """
    匹配结尾，符合163邮箱找出来
    :return:
    """
    # 符合163邮箱找出来
    email_list = ["xiaoWang@163.com", "xiaoWang@163.comheihei", "com.xiaowang@qq.com"]
    for email in email_list:
        ret = re.match(r"\w{4,20}@163\.com$", email) # 匹配的字符串中出现了正则的符号要使用转移符号\
        if ret:
            print(f'{ret.group()} 为163邮箱{email}')
        else:
            print(f'{email} 不符合163邮箱')
    print('-' * 50)
    # 匹配0-99
    ret = re.match(r"[1-9]?\d$", "09")
    if ret:
        print(ret.group())
    else:
        print("未匹配成功，非0-99之间")

def split_group():
    # 匹配0-100
    ret = re.match(r"[1-9]?\d$|100", "78")
    if ret:
        print(ret.group())
    else:
        print("未匹配成功，非0-100之间")

    # 匹配1-99
    ret = re.match(r"[1-9]?[1-9]$|10", "10")
    if ret:
        print(ret.group())
    else:
        print("未匹配成功，非1-99之间")

    print('_' * 50)
    ret = re.match(r"\w{4,20}@(163|126|qq\.com)", "test@126.com")
    print(ret.group())
    print('-' * 50)
    ret = re.match(r"([^-]*)-(\d+)", "010-12345678") # ^代表没遇到-就一直吃
    print(ret.group())
    print(ret.group(1))
    print(ret.group(2))
    print('-' * 50)
    ret = re.match(r"<[a-zA-Z]*>\w*</[a-zA-Z]*>", "<html>hh</htmla>")
    print(ret.group())
    ret = re.match(r"<([a-zA-Z]*)>\w*</\1>", "<html>hh</html>")
    print(ret.group())
    print('-' * 50)
    labels = ["<html><h1>www.cskaoyan.com</h1></html>", "<html><h1>www.cskaoyan.com</h2></html>"]
    for label in labels:
        ret = re.match(r"<(\w*)><(\w*)>.*</\2></\1>", label)
    if ret:
        print("%s 是符合要求的标签" % ret.group())
    else:
         print("%s 不符合要求" % label)

def other_func():
    """
    search findall sub split
    :return:
    """
    ret = re.search(r"\d+", "阅读次数为 9999")
    print(ret.group())
    print('-' * 50)
    ret = re.findall(r"\d+", "python = 9999, c = 7890, c++ = 12345")
    print(ret)

def find_second_match(pattern, text):
     matches = re.finditer(pattern, text)
     try:
        next(matches)  # 跳过第一个匹配项
        next(matches)
        second_match = next(matches)  # 获取第二个匹配项
        return second_match.group()
     except StopIteration:
             return None

def number_generator(start = 0):
    while start <= 5:  # 无限循环，持续生成数字
        yield start
        start += 1
    return



def use_generator():
    gen = number_generator() # 创建生成器实例 从0开始
    # print(next(gen))  # 输出0
    # print(next(gen))  # 输出1
    # print(next(gen))  # 输出2
    for i in gen:
        print(i)


if __name__ == '__main__':
    # simple()
    # single()
    # more_alp()
    # start_end()
    # split_group()
    # other_func()
    # 示例用法
    # text = "abc123def456ghi789"
    # pattern = r"\d+"
    # second_match = find_second_match(pattern, text)
    # print(second_match)
    # print(gen)
    use_generator()